﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 第七次课堂作业
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Pen myPen = Pens.Blue;

        public Form1()
        {
            InitializeComponent();
        }

        //点击按钮进行绘画
        private void btnDraw_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = this.CreateGraphics();
            int n = 10;
            double leng = 100;
            functionN();
            functionLength();
            drawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
        }

        //递归进行绘画
        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            functionTh1();
            functionTh2();
            functionPer1();
            functionPer2();


            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        //左分支
        private void functionPer2()
        {
            try
            {
                if (textPer2.Text != "")
                {
                    th1 = Convert.ToDouble(textPer2.Text);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("左分支长度比有误！");
                Environment.Exit(0);
            }
        }

        //右分支
        private void functionPer1()
        {
            try
            {
                if (textPer1.Text != "")
                {
                    th1 = Convert.ToDouble(textPer1.Text);
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("右分支长度比有误！");
                Environment.Exit(0);
            }
        }

        //选择画笔颜色
        private void changePen()
        {
            try
            {
                if (green.Checked == true)
                {
                    myPen = Pens.Green;
                }
                else if (red.Checked == true)
                {
                    myPen = Pens.Red;
                }
                else if (black.Checked == true)
                {
                    myPen = Pens.Black;
                }
                else if (yellow.Checked == true)
                {
                    myPen = Pens.Yellow;
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("颜色选择错误！");
                Environment.Exit(0);
            }
        }

        //长度
        private void functionLength()
        {
            try
            {
                if (textLeng.Text != "")
                {
                    th1 = Convert.ToDouble(textLeng.Text);
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("主干长度有误！");
                Environment.Exit(0);
            }
        }

        private void functionN()
        {
            try
            {
                if (textN.Text != "")
                {
                    th1 = Convert.ToInt16(textN.Text);
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("递归深度有误！");
                Environment.Exit(0);
            }
        }

        //右分支角度
        void functionTh1()
        {
            try
            {
                if (textTh1.Text != "")
                {
                    th1 = Convert.ToDouble(textTh1.Text);
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("右分支角度有误！");
                Environment.Exit(0);
            }
        }

        //左分支角度
        private void functionTh2()
        {
            try
            {
                if (textTh2.Text != "")
                {
                    th1 = Convert.ToDouble(textTh2.Text);
                }
            }
            catch (Exception a)
            {
                MessageBox.Show("左分支角度有误！");
                Environment.Exit(0);
            }
        }

        private void drawLine(double x0, double y0, double x1, double y1)
        {
            changePen();
            graphics.DrawLine(
                myPen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }
    }
}
